package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.model.entity.TArea;
import com.example.model.entity.TEquipment;
import com.example.model.vo.AreaTree;
import com.example.service.TAreaService;
import com.example.service.TEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  前端控制器
 *
 * @author zhangr
 * @Description Created on 2020-12-11
 */
@Controller
@RequestMapping("tArea")
@RestController
@CrossOrigin
@Api(value = "区域管理",tags={"区域管理接口"})
public class TAreaController  {

    @Autowired
    private TAreaService areaService;

    @Autowired
    private TEquipmentService equipmentService;
    /**
     * 获取区域树
     * @return
     */
    @ApiOperation(value = "获取区域树", notes = "查询当前用户权限的区域树", httpMethod = "GET")
    @GetMapping("/tree")
    public ApiResult<List<AreaTree>> getOrgTree(){
        try {
            return areaService.getAreaTree();
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_CUSTOMER_FALSE);
        }
    }

    /**
     * 获取所有区域
     * @return
     */
    @ApiOperation(value = "获取所有区域", notes = "查询获取所有区域", httpMethod = "GET")
    @GetMapping("/getList")
    public ApiResult<List<TArea>> getList(){
        try {
            return areaService.getList();
        }catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_PAGE_CUSTOMER_FALSE);
        }
    }
    /*
     * 新增
     *
     */
    @ApiOperation(value = "新增区域", notes = "新增区域", httpMethod = "POST")
    @PostMapping("/save")
    public ApiResult<Boolean> save(@RequestBody TArea area){
        try{
            return areaService.saveT(area);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }

    /*
     * 修改
     *
     */
    @ApiOperation(value = "修改区域", notes = "修改区域", httpMethod = "POST")
    @PostMapping("/modify")
    public ApiResult<Boolean> modify(@RequestBody TArea area){
        try{
            return new ApiResult<>(areaService.updateById(area));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_SAVE_AD_FALSE);
        }
    }

    /*
     * 删除
     *
     */
    @ApiOperation(value = "删除区域", notes = "删除区域", httpMethod = "GET")
    @GetMapping("/del/{id}")
    public ApiResult<Boolean> del(@PathVariable("id") String id){
        try {
            QueryWrapper<TEquipment> equipmentQueryWrapper = new QueryWrapper<>();
            equipmentQueryWrapper.lambda().eq(TEquipment::getIsValid,1L)
                    .eq(TEquipment::getAreaId,id);
            int num =  equipmentService.count(equipmentQueryWrapper);
            if(num > 0){
                return  new ApiResult<>(-231,"该区域有配置设备，不允许删除");
            }
            QueryWrapper<TArea> areaQueryWrapper = new QueryWrapper<>();
            areaQueryWrapper.lambda().eq(TArea::getIsValid,1L)
                    .eq(TArea::getParentId,id);
             num = areaService.count(areaQueryWrapper);
             if(num >0){
                 return new ApiResult<>(-232,"该区域存在下级区域，不允许删除");
             }
            QueryWrapper<TArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TArea::getAreaId,id);
            TArea obj =new TArea();
            obj.setIsValid(0L);
            return new ApiResult<>(areaService.update(obj,queryWrapper));
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.MALL_SERVICE_ORDER_GET_FALSE);
        }
    }
}
